/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.escom.healthlc.security;

import com.escom.healthlc.utils.Constantes;
import javax.faces.application.Resource;
import javax.faces.application.ResourceHandler;
import javax.faces.application.ResourceHandlerWrapper;
import javax.faces.context.FacesContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Carlos
 */
public class ResourceHandlerWrapperImpl extends ResourceHandlerWrapper {

    private static final Logger logger = LoggerFactory.getLogger(ResourceHandlerWrapperImpl.class);

    private final ResourceHandler wrapped;

    public ResourceHandlerWrapperImpl(ResourceHandler wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public ResourceHandler getWrapped() {
        return wrapped;
    }

    @Override
    public Resource createResource(final String resourceName, final String libraryName) {
        if ("css".equals(libraryName) || "primefaces-aristo".equals(libraryName)
                || "primefaces".equals(libraryName) || "images".equals(libraryName)
                || "primefaces-extensions-uncompressed".equals(libraryName)) {
            String pathApplication = FacesContext.getCurrentInstance().getExternalContext().getRealPath("");
            return new MediaResource(libraryName, resourceName, pathApplication);

        } else {
            return super.createResource(resourceName, libraryName);
        }
//        if ("css".equals(libraryName) || "primefaces-aristo".equals(libraryName) 
//                || "primefaces".equals(libraryName) || "images".equals(libraryName)) {
//            String pathApplication = FacesContext.getCurrentInstance().getExternalContext().getRealPath("");
//            return new MediaResource(libraryName, resourceName, pathApplication);
//        } else {
//            return super.createResource(resourceName, libraryName);
//        }
    }

    /**
     * @param libraryName
     * @return
     * @see
     * javax.faces.application.ResourceHandlerWrapper#libraryExists(java.lang.String)
     */
    @Override
    public boolean libraryExists(String libraryName) {
//        logger.debug("libraryExists libraryName: {}", libraryName);
        if ("css".equals(libraryName)) {
            return true;
        } else {
            return super.libraryExists(libraryName);
        }
    }

    /**
     * @param context
     * @return
     * @see
     * javax.faces.application.ResourceHandlerWrapper#isResourceRequest(javax.faces.context.FacesContext)
     */
    @Override
    public boolean isResourceRequest(FacesContext context) {
//        logger.debug("isResourceRequest");
        return super.isResourceRequest(context);
    }

}
